package com.lemon.rpcframe.dao.behavior;

import java.util.List;

import com.lemon.rpcframe.annotation.ServiceBehavior;
import com.lemon.rpcframe.dao.DBHelper;
import com.lemon.rpcframe.dao.Total;
import com.lemon.rpcframe.dao.contract.ITaskLogService;
import com.lemon.rpcframe.dao.entity.TaskExecutableBean;
import com.lemon.rpcframe.dao.entity.TaskLogBean;
import com.lemon.rpcframe.util.StringUtils;

@ServiceBehavior
public class TaskLogServiceImpl implements ITaskLogService {

    public static final int DEFAULT_PAGESIZE = 30;

    @Override
    public long insert(TaskLogBean entity) throws Exception {
        if (null == entity) {
            return 0l;
        }
        long id = 0l;
        Object obj = DBHelper.getInstance().getdaoHelper().sql.insert(entity);
        if (null != obj) {
            id = (long) obj;
        }
        return id;
    }

    @Override
    public void insertBatch(List<TaskLogBean> entitys) throws Exception {
        if (null == entitys) {
            return;
        }
        for (TaskLogBean joblog : entitys) {
            DBHelper.getInstance().getdaoHelper().sql.insert(joblog);
        }
    }

    @Override
    public List<TaskLogBean> loadPageByTaskGroup(String taskTrackerGroup, int pageSize, int page, Total<Integer> total) throws Exception {

        String where = "";
        if (!StringUtils.isNullOrEmpty(taskTrackerGroup)) {
            where = String.format(" taskGroup='%s'", taskTrackerGroup);
        }
        total.setTotal(countByTaskGroup(taskTrackerGroup));
        List<TaskLogBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByPage(TaskLogBean.class, "", "", page, pageSize, " createDate desc");
        return list;
    }

    @Override
    public int countByTaskGroup(String taskTrackerGroup) throws Exception {
        String where = "";
        if (!StringUtils.isNullOrEmpty(taskTrackerGroup)) {
            where = String.format(" taskGroup='%s'", taskTrackerGroup);
        }
        int cnt = DBHelper.getInstance().getdaoHelper().sql.getCount(TaskExecutableBean.class, where);
        return 0;
    }

}
